package cn.cyh.org.service.impl;

import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.org.domain.Department;
import cn.cyh.org.mapper.DepartmentMapper;
import cn.cyh.org.query.DepartmentQuery;
import cn.cyh.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
// 支持事务 (如果有事务就加入事务，没有就不加入)
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    //  自动注入
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    @Transactional // 支持事务
    // 添加方法
    public void save(Department department) {
        // 返回自增id
        departmentMapper.save(department);
        this.update(department);
    }


    @Override
    @Transactional
    // 修改方法
    public void update(Department department) {
        // 将dirpath封装
        String dirPath = "";
        // 如果是顶级部门
        if(department.getParent()==null){// 如果是空就封装自己的id
            dirPath = "/"+department.getId();

        } else {
            // 获取父级部门id
            Department department1 = departmentMapper.loadById(department.getParent().getId());
            // 进行封装
            dirPath = department1.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        // 然后修改回显
        departmentMapper.update(department);
    }

    // 部门树查询
    @Override
    public List<Department> deptTree() {
        return departmentMapper.deptTree();
    }


}
